package com.xcl.controller;

import com.xcl.pojo.XclTailAfter;
import com.xcl.service.LoadingServiceInter;
import com.xcl.service.XclTailAfterServiceInter;
import com.zzg.entity.Driver;
import com.zzg.entity.Tailafter;
import com.zzg.entity.Transportprotocol;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName LoadingController
 * @Description 装车配载控制器
 * @Author: xiaochaolei
 * @date 2021/6/28 11:14
 * @Version 1. 0
 */
@Controller
public class LoadingController {
    @Autowired
    LoadingServiceInter loadingServiceInter;
    @Autowired
    XclTailAfterServiceInter xclTailAfter;
    /**
     *得到未装载的数据
     * @param page 页码数
     * @param limit 条目数
     * @param waybill_id 运单号
     * @param substationUnit 到达分站
     * @param modeTransport 运输方式
     * @param startDate 起始日期
     * @param endingDate 到达日期
     * @return map类型未装载的集合
     */
    @GetMapping (value = "/getLoadingBYStateOne", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getLoadingBYStateOne(int page,int limit,String waybill_id,String substationUnit,String modeTransport,String startDate,String endingDate,String id){
        //定义一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        if (id==null){
            map.put("count",loadingServiceInter.getLoadingBYStateOne().size());
            map.put("data",loadingServiceInter.getLoadingBYStateOneLimit(page,limit));
            return map;
        }else {
            map.put("count",loadingServiceInter.getLoadingByWhere(waybill_id,substationUnit,modeTransport,startDate,endingDate,page,limit).size());
            map.put("data",loadingServiceInter.getLoadingByWhere(waybill_id,substationUnit,modeTransport,startDate,endingDate,page,limit));
            return map;
        }

    }


    /**
     *得到运输中的数据
     * @param page 页码数
     * @param limit 条目数
     * @param waybill_id 运单号
     * @param modeTransport 运输方式
     * @param id 判断是否为高级查询
     * @return map类型运输中的集合
     */
    @GetMapping (value = "/getLoadingBYStateTwo", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getLoadingBYStateTwo(int page,int limit,String waybill_id,String  modeTransport,String id){
        //定义一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        if (id==null){
            map.put("count",loadingServiceInter.getLoadingBYStateTwo().size());
            map.put("data",loadingServiceInter.getLoadingBYStateTwoLimit(page,limit));
            return map;
        }else {
            map.put("count", loadingServiceInter.getLoadingByWhereTwo(waybill_id,modeTransport,page,limit).size());
            map.put("data", loadingServiceInter.getLoadingByWhereTwo(waybill_id,modeTransport,page,limit));
            return map;
        }
    }

    /**
     *得到装载中的数据
     * @param page 页码数
     * @param limit 条目数
     * @return map类型装载中的集合
     */
    @GetMapping (value = "/getLoadingBYStateThree", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getLoadingBYStateThree(int page,int limit){
        //定义一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        map.put("count",loadingServiceInter.getLoadingBYStateThree().size());
        map.put("data",loadingServiceInter.getLoadingBYStateThreeLimit(page,limit));
        return map;
    }
    /**
     * 点击新增时将 运单id以及货运id存到session中
     * @param waybill_id  运单id
     * @param freightInformation 货运id
     * @param request 请求
     * @return 是否成功
     */
    @PostMapping(value = "/getWaybill_idAndFreightInformation", produces = "application/text;charset=utf-8")
    @ResponseBody
    public String getWaybill_idAndFreightInformation(String waybill_id, String freightInformation, HttpServletRequest request){
        //将表单中的运单id和货运id保存到session中
        request.getSession().setAttribute("Waybill_id",waybill_id);
        request.getSession().setAttribute("freightInformation",freightInformation);
        //返回
        return "成功";
    }


    /**
     * 新增车辆信息和运输协议
     * @param driver 车辆信息
     * @param request 请求
     * @param transportprotocol 运输协议信息
     * @return 是否成功
     */
    @PostMapping(value = "/insertDriverTransportprotocol", produces = "application/text;charset=utf-8")
    @ResponseBody
    public String insertDriverTransportprotocol(Driver driver, HttpServletRequest request,Transportprotocol transportprotocol){
        //得到id
        String id= UuidUtil.getUuid();
        //设置id
        driver.setDriverId(id);
        //将id设置到session中
        request.getSession().setAttribute("insertDriver",driver.getDriverId());
        //从session中得到货运信息id强转为string并且赋值
        String freightInformation=(String)request.getSession().getAttribute("freightInformation");
        //设置协议表中的货运信息
        transportprotocol.setFreightinformation(freightInformation);
        //从session中得到运单id强转为string并且赋值
        String waybill_id=(String)request.getSession().getAttribute("Waybill_id");

        //选择运单跟踪
        Tailafter tailafter=new Tailafter();
        //设置跟踪信息的id
        tailafter.setTailafter(UuidUtil.getUuid());
        //获得当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String hehe = dateFormat.format( now );
        //设置跟踪时间
        tailafter.setTailafterTime(hehe);
        //设置运单号
        tailafter.setWaybillId(waybill_id);
        //设置状态
        tailafter.setState("运输中");
        //选择跟踪信息
        xclTailAfter.insertXclTailAfter(tailafter);

        //设置到协议表中的运单id
        transportprotocol.setWaybillId(waybill_id);
        //新增协议和车辆信息
        loadingServiceInter.insertDriver(driver,transportprotocol);
        //修改状态为运输中
        loadingServiceInter.upteModification(waybill_id);
        //返回
        return "成功";
    }


    /**
     * 多条修改
     * @param strings 需要修改的数据集合
     */
    @PostMapping(value = "/ModificationOne", produces = "application/text;charset=utf-8")
    @ResponseBody
    public void ModificationOne(String strings){
        //根据字符串分割
        String[] split= strings.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("装车中");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        loadingServiceInter.upteModificationOne(split);
    }


    /**
     * 多条修改
     * @param strings 需要修改的数据集合
     */
    @PostMapping(value = "/upteModificationTwo", produces = "application/text;charset=utf-8")
    @ResponseBody
    public void upteModificationTwo(String strings){
        //根据字符串分割
        String[] split= strings.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("装车完毕");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        loadingServiceInter.upteModificationTwo(split);
    }


    /**
     * 多条修改
     * @param strings 需要修改的数据集合
     */
    @PostMapping(value = "/upteModificationThree", produces = "application/text;charset=utf-8")
    @ResponseBody
    public void upteModificationThree(String strings){
        //根据字符串分割
        String[] split= strings.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("未装载");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        loadingServiceInter.upteModificationThree(split);
    }


    /**
     * 自动填充司机名称
     * @return map集合
     */
    @PostMapping(value = "/getOwnerNameByDriver", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getOwnerNameByDriver(){
        Map<String,Object> map = new HashMap<>();
        map.put("index",0);
        map.put("data",loadingServiceInter.getOwnerNameByDriver());
        //返回map集合
        return map;
    }

    /**
     * 根据车辆id查询车辆信息
     * @param id 车辆id
     * @return 车辆信息
     */
    @PostMapping(value = "/getDriverByDriverId", produces = "application/json;charset=utf-8")
    @ResponseBody
    public  Map<String,Object>  getDriverByDriverId(String id){
        Map<String,Object> map = new HashMap<>();
        map.put("data",loadingServiceInter.getDriverByDriverId(id));
        //返回map集合
        return map;
    }
}
